var app = getApp();
var ux = require('../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:0,
    couponId: null,
    list: [],
    winheight: 0,
    winwidth: 0,
    merchant: null,
    action: "/api/memberCoupon/getOrderCouponLists.do",
    submit: "{{submit}}"
  },
  toShopDetail: function (event) {
    var dataset = event.currentTarget.dataset;
    var shopid = dataset.shopid;
    wx.navigateTo({
      url: '/pages/member/tab1/shop/detail?id=' + shopid,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth;
        self.setData({
          winheight: clientHeight,
          winwidth: clientWidth
        });
      }
    });
    this.setData({
      couponId: options.couponId,
      orderId: options.orderId,
      merchant: wx.getStorageSync("banrdId")
    });
    this.getList();
  },
  loadMore: function () {
    this.getList();
  },
  getList: function () {
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var self=this;
    var params = { orderId: this.data.orderId };
    ux.request(self.data.action, params, function (data) {
      wx.hideLoading();
      var datas = data.data.list;
      for (var i = 0; i < datas.length; i++) {
        if (datas[i].id == self.data.couponId) {
          datas[i].select=1;
        }
      }
      self.setData({
          list:datas
        })
    }, true);
  },
  toDetailss: function (event) {
    var dataset = event.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/member/tab1/shop/detail?id=' + dataset.goodid,
    })
  },
  onForm: function (event) {
    if (this.data.submit) {
      var $this = this;
      var dataset = event.currentTarget.dataset;
      ux.request(this.data.submit, detail.value, function (protocol) {
        if (protocol.success) {
          if (dataset.url) {
            wx.redirectTo(dataset.url);
          }
        }
      }, true);
    }
  },
  onBar: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val + "" != "") {
      if (dataset.val != this.data.tab) {
        if (this.data.list[dataset.val].length > 0) {
          data[dataset.key] = dataset.val;
        } else {
          data[dataset.key] = dataset.val;
          this.getList();
        }
      } else {
        data[dataset.key] = dataset.val;
      }

    }
    this.setData(data);
  },
  onTap: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val + "" != "") {
      data[dataset.key] = dataset.val;
    }
    this.setData(data);
  },
  toGetCoupon: function () {
    wx.navigateTo({
      url: '/pages/member/tab3/coupon/getCouponList',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      isShow: true
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  selects: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    var lists = this.data.list;
    if(lists[index].select==1){
      lists[index].select = 0;
    }else{
      for (var i = 0; i < lists.length; i++) {
        lists[i].select = 0;
      }
      lists[index].select = 1;
    }
    this.setData({
      list: lists
    })
  },
  toGoconfirm:function(){
    var lists=this.data.list;
    var index=1000;
    for (var i = 0; i < lists.length; i++) {
      if (lists[i].select==1){
           index=i;
         }
    }
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面
    var goods = prevPage.data.list;
    var ydi = 0;
    if(index!=1000){
      for (var i = 0; i < goods.length; i++) {
        if (goods[i].goodsNumber == lists[index].commodity) {
          ydi = ux.acMul(goods[i].price, 1-lists[index].discount / 10)
        }
      }
      prevPage.setData({
        pdi:0,
        yeList: [],
        selectGood: lists[index].commodity,
        sumff: ux.acSub(ux.acAdd(prevPage.data.sumDouble, prevPage.data.peisong), ydi),
        sum: ux.acSub(prevPage.data.sumDouble, ydi),
        ydi: ydi,
        couponId: lists[index].id
      })
    }else{
      prevPage.setData({
        pdi: 0,
        yeList: [],
        selectGood:0,
        sumff: ux.acAdd(prevPage.data.sumDouble, prevPage.data.peisong),
        sum: ux.acAdd(prevPage.data.sumDouble),
        ydi: ydi,
        couponId: null
      })
    }
    wx.navigateBack({
      delta: 1
    })
  }
})
